package com.lwq.nacos.grayscale.config;

import com.alibaba.cloud.nacos.ribbon.NacosServerList;
import com.lwq.nacos.grayscale.filter.ServletHeaderFilter;
import com.lwq.nacos.grayscale.gray.CustomNacosMetadataRule;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.cloud.netflix.ribbon.RibbonClientConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;

@Configuration
@ConditionalOnClass(NacosServerList.class)
@AutoConfigureBefore(RibbonClientConfiguration.class)
public class NacosGrayAutoConfiguration {

    @Bean
    @ConditionalOnMissingBean
    @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
    public CustomNacosMetadataRule customEurekaMetadataRule() {
        return new CustomNacosMetadataRule();
    }



    @Bean
    public FilterRegistrationBean grayHeaderNameFilter(){
        FilterRegistrationBean filterRegistration = new FilterRegistrationBean();
        filterRegistration.setFilter(new ServletHeaderFilter());
        filterRegistration.addUrlPatterns("/*");
        filterRegistration.setOrder(0);
        return filterRegistration;
    }


}
